﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
using NetJsWire.Lite.Designers;
using NetJsWire.Lite.Widgets.UI.Layout;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Form
{
    [JsonObject( MemberSerialization = MemberSerialization.OptIn )]
    [Designer( typeof( WidgetDesigner ) )]
    public class CompositeField : FieldLW, IContainerControl
    {
        private const string xtype = "compositefield";
        private readonly HBoxLayoutEngine _layoutEngine = new HBoxLayoutEngine();
        private Control _activeControl;

        public CompositeField()
            : this( null )
        {
        }

        public CompositeField( IContainer container )
            : base( container )
        {
            base.Xtype = xtype;
            base.BackColor = DefaultBackgroundColor;
            LabelConnector = ", ";

            Invalidated += new InvalidateEventHandler( CompositeField_Invalidated );
        }

        void CompositeField_Invalidated( object sender, InvalidateEventArgs e )
        {
            if(Parent == null )
            {
                return;
            }

            Parent.Invalidate();
        }

        [Browsable( false )]
        [EditorBrowsable( EditorBrowsableState.Never )]
        [DefaultValue( null )]
        [JsonProperty( "items", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        public virtual IEnumerable Items
        {
            get
            {
                return Controls.Count > 0 ? Controls.OfType<BaseComponent>().Where( c => !( c is ButtonLW ) || !( ( ButtonLW ) c ).InButtons ) : null;
            }
        }

        [JsonProperty( "xtype", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include )]
        [DefaultValue( xtype )]
        public override string Xtype
        {
            get
            {
                return xtype;
            }
            set
            {
            }
        }

        [JsonProperty( "labelConnector", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include )]
        [DefaultValue( ", " )]
        [Description( "The string to use when joining segments of the built label together (defaults to ', ')" )]
        public string LabelConnector { get; set; }

        public override LayoutEngine LayoutEngine
        {
            get
            {
                return _layoutEngine;
            }
        }

        #region Implementation of IContainerControl

        /// <summary>
        ///   Activa un control especificado.
        /// </summary>
        /// <returns>
        ///   true si el control se activa correctamente; en caso contrario, false.
        /// </returns>
        /// <param name = "active"><see cref = "T:System.Windows.Forms.Control" /> que se está activando. </param>
        /// <filterpriority>1</filterpriority>
        public bool ActivateControl( Control active )
        {
            if ( Controls.Contains( active ) )
            {
                // Select the control and scroll the control into view if needed.
                active.Select();
                _activeControl = active;
                return true;
            }
            return false;
        }

        /// <summary>
        ///   Obtiene o establece el control que está activo en el control contenedor.
        /// </summary>
        /// <returns>
        ///   <see cref = "T:System.Windows.Forms.Control" /> que está activo actualmente en el control contenedor.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Advanced )]
        [DefaultValue( null )]
        public Control ActiveControl
        {
            get { return _activeControl; }
            set
            {
                if ( Controls.Contains( value ) )
                {
                    _activeControl = value;
                }
            }
        }

        #endregion
    }
}
